package edu.nf.relaxenjoy.service.impl;

import com.alibaba.fastjson.JSON;
import edu.nf.relaxenjoy.common.constant.Constant;
import edu.nf.relaxenjoy.common.constant.platform.PlatformConstant;
import edu.nf.relaxenjoy.common.exception.CommonException;
import edu.nf.relaxenjoy.common.util.ConvertUtils;
import edu.nf.relaxenjoy.common.util.JsonUtils;
import edu.nf.relaxenjoy.common.util.RedisUtils;
import edu.nf.relaxenjoy.domain.entity.AgreeCollect;
import edu.nf.relaxenjoy.domain.entity.Album;
import edu.nf.relaxenjoy.domain.entity.AlbumRelation;
import edu.nf.relaxenjoy.domain.entity.Post;
import edu.nf.relaxenjoy.mapper.AgreeCollectMapper;
import edu.nf.relaxenjoy.mapper.AlbumMapper;
import edu.nf.relaxenjoy.mapper.AlbumRelationMapper;
import edu.nf.relaxenjoy.mapper.PostMapper;
import edu.nf.relaxenjoy.service.AlbumRelationService;
import edu.nf.relaxenjoy.domain.dto.AlbumRelationDTO;
import edu.nf.relaxenjoy.domain.vo.UserRecordVo;
import edu.nf.relaxenjoy.web.websocket.WebsocketServer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhu
 * @date 2024-12-17
 */
@Service
@RequiredArgsConstructor
public class AlbumRelationServiceImpl implements AlbumRelationService {

    private final RedisUtils redisUtils;

    private final AlbumRelationMapper albumRelationMapper;

    private final AlbumMapper albumMapper;

    private final PostMapper postMapper;

    private final AgreeCollectMapper agreeCollectMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAlbumRelation(AlbumRelationDTO albumRelationDTO) {
        String postListKey = PlatformConstant.POST_LIST_KEY;

        if (redisUtils.hExists(postListKey, String.valueOf(albumRelationDTO.getMid()))) {
            Post post = JsonUtils.parseObject((String) redisUtils.hGet(postListKey, String.valueOf(albumRelationDTO.getMid())), Post.class);
            post.setCollectionCount(post.getCollectionCount() + 1);
            redisUtils.hPut(postListKey, String.valueOf(albumRelationDTO.getMid()), JsonUtils.toJsonString(post));
        }

        String albumRelationKey = PlatformConstant.ALBUM_RELATION_KEY + albumRelationDTO.getMid();

        boolean isMember = isCollectPostToAlbum(String.valueOf(albumRelationDTO.getUid()), String.valueOf(albumRelationDTO.getMid()));
        if (!isMember) {
            redisUtils.sAdd(albumRelationKey, String.valueOf(albumRelationDTO.getUid()));
        }

        //如果当前图片是本人发布的则无法添加至专辑中
        Post post = postMapper.getById(albumRelationDTO.getMid());
        if (post.getUserId().equals(albumRelationDTO.getUid())) {
            return;
        }

        AlbumRelation albumRelation = ConvertUtils.sourceToTarget(albumRelationDTO, AlbumRelation.class);
        albumRelationMapper.insert(albumRelation);

        //查找当前专辑
        post.setCollectionCount(post.getCollectionCount() + 1);
        postMapper.updateById(post);

        Album album = albumMapper.getById(albumRelationDTO.getAid());
        Long imgCount = album.getImgCount() + post.getCount();
        album.setImgCount(imgCount);
        album.setCollectionCount(album.getCollectionCount() + 1);
        albumMapper.updateById(album);

        AgreeCollect agreeCollect = new AgreeCollect();
        agreeCollect.setUid(albumRelationDTO.getUid());
        agreeCollect.setAgreeCollectId(albumRelationDTO.getMid());
        agreeCollect.setAgreeCollectUid(post.getUserId());
        agreeCollect.setType(2);
        agreeCollectMapper.insert(agreeCollect);

        String userRecordKey = PlatformConstant.USER_RECORD + post.getUserId();

        if (Boolean.TRUE.equals(redisUtils.hasKey(userRecordKey)) && !albumRelationDTO.getUid().equals(post.getUserId())) {
            //如果当前点赞的用户是本用户不需要通知
            UserRecordVo userRecordVo = JsonUtils.parseObject(redisUtils.get(userRecordKey), UserRecordVo.class);
            userRecordVo.setAgreeCollectionCount(userRecordVo.getAgreeCollectionCount() + 1);
            redisUtils.set(userRecordKey, JSON.toJSONString(userRecordVo));
            try {
                WebsocketServer.sendMessageTo(JSON.toJSONString(userRecordVo), String.valueOf(agreeCollect.getAgreeCollectUid()));
            } catch (Exception e) {
                throw new CommonException(Constant.MSG_ERROR);
            }
        }
    }

    @Override
    public boolean isCollectPostToAlbum(String uid, String mid) {
        String albumImgRelationKey = PlatformConstant.ALBUM_RELATION_KEY + mid;

        return redisUtils.sIsMember(albumImgRelationKey, uid);
    }

    @Override
    public void deleteAlbumRelation(AlbumRelationDTO albumRelationDTO) {
        AlbumRelation albumRelationEntity = albumRelationMapper.getOne(albumRelationDTO.getAid(), albumRelationDTO.getMid());
        albumRelationMapper.remove(albumRelationEntity);
    }
}
